<template>
  <div class="home_container">
    <!-- 标题导航栏 -->
    <van-nav-bar fixed>
      <template #left>
        <div class="logo"></div>
      </template>
      <template #right>
        <van-button
          round
          icon="search"
          type="info"
          @click="$router.push({ path: '/search' })"
        >
          搜索
        </van-button>
      </template>
    </van-nav-bar>
    <!-- 标签导航栏 -->
    <van-tabs v-model="active" @click="articlesClick">
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell
              v-for="(item, index) in list"
              :key="index"
              :title="item.title"
              @click="
                $router.push({ path: '/article', query: { id: item.art_id } })
              "
            >
              <template #label>
                <van-grid :border="false" :column-num="item.cover.type">
                  <van-grid-item
                    v-for="(img, index) in item.cover.images"
                    :key="index"
                  >
                    <van-image :src="img" lazy-load fit="cover" />
                  </van-grid-item>
                </van-grid>
                <div class="text">
                  <span>{{ item.aut_name }}</span>
                  <span>{{ item.comm_count }}评论</span>
                  <span>{{ item.pubdate | showDate }}</span>
                </div>
              </template>
            </van-cell>
          </van-list>
        </van-pull-refresh>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { getUserChannels, getArticles } from "./home";
import moment from "moment";

// 中文格式
moment.locale("zh-cn");
export default {
  name: "home",
  data() {
    return {
      active: 0,
      list: [], //
      loading: false, //上拉刷新
      finished: false, //数据全部渲染完成
      isLoading: false, //下拉刷新
      channels: [], //频道列表
      timestamp: null //时间戳
    };
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      if (!this.timestamp) {
        this.timestamp = Date.now();
      }
      let res = await getArticles({
        channel_id: this.channels[this.active].id,
        timestamp: this.timestamp
      });
      this.timestamp = res.data.data.pre_timestamp;
      this.list.push(...res.data.data.results);

      // 加载状态结束
      this.loading = false;

      // 数据全部加载完成
      if (this.list.length >= 100) {
        this.finished = true;
      }
    },
    onRefresh() {
      // 不管怎样，都先将finished的值转为false,不管怎样都可以渲染数据
      this.finished = false;
      this.list = [];
      //先设置上拉加载数据为true
      this.loading = true;
      this.onLoad();
      this.isLoading = false;
    },
    articlesClick() {
      this.finished = false;
      this.list = [];
      this.loading = true;
      this.onLoad();
    }
  },
  async created() {
    let res = await getUserChannels();
    this.channels = res.data.data.channels;
  },
  filters: {
    showDate(value) {
      return moment(value).fromNow();
    }
  }
};
</script>

<style lang="less">
* {
  box-sizing: border-box;
}
.home_container {
  height: 100vh;
  padding-top: 46px;

  .logo {
    width: 100px;
    height: 30px;
    background: url("~@/assets/logo.png") no-repeat;
    background-size: cover;
  }
  .van-button__content {
    .van-icon {
      color: #fff;
    }
  }
  .van-tabs {
    height: 100%;
  }
  .van-tabs__wrap {
    width: 100%;
    position: fixed;
    top: 46px;
    z-index: 10;
  }
  .van-tabs__content {
    height: 100%;
    padding-top: 46px;
    overflow-y: scroll;
  }
  .text {
    span {
      line-height: 10px;
      margin: 0 5px;
    }
  }
}
</style>
